/*---------------页面初始化时列表跳至最底bug解决-------------------*/
window.onload = function(){
	$('.mui-scroll').css('transform','translate3d(0px,0px,0px) !important');
	setTimeout(function(){
		$('.mui-scroll').css('transform','translate3d(0px,0px,0px)');
	},0);
}

var ajaxStatus = null;
var ajaxId = null;
//类目筛选
$('.leftShaiXuan').on('click',function(){
	if($(this).attr("hide") == "true"){
		$('.firstCategory').show();
		$('#secondPopover').hide();
		$(this).attr("hide","false")
		$('.rightShaiXuan').attr("hide","true")
	}else{
		$('.firstCategory').hide();
		$(this).attr("hide","true")
	}
})
//商品状态筛选
$('.rightShaiXuan').on('click',function(){
	if($(this).attr("hide") == "true"){
		$('#secondPopover').show();
		$('.firstCategory').hide();
		$('.leftShaiXuan').attr("hide","true")
		$(this).attr("hide","false")
	}else{
		$('#secondPopover').hide();
		$(this).attr("hide","true")
	}
})

//商品类目筛选后商品列表
$('.firstCategory').find('li').on('click',function(){
	$(this).find('ul').show();
	$(this).siblings().find('ul').hide();
})

$('.firstCategory').find('li ul li').on('click',function(){
	var _this = $(this);
	var categoryId = $(this).attr("categoryId");
	$('.leftShaiXuan').find('b').html($(this).html());
	$('.leftShaiXuan').find('b').attr('categoryId',categoryId);
	var status = $('.rightShaiXuan').find('b').attr('status');
	ajaxStatus = $('.rightShaiXuan').find('b').attr('status');
	$('#secondPopover').hide();
	$('.rightShaiXuan').attr("hide","true");
	$('.leftShaiXuan').attr("hide","true");
	$.ajax({
		type:"POST",
		url : "/maidian/item/itemListMore.htm",
		dataType : "html",
		data : {
			"status" :status,
			"categoryId" :categoryId
		},
		success : function(data) {
			$('.ulWrap').html(data);
			_this.parent().parent().parent().hide();
			_this.parent().hide();
			
	}
	});
})
//不选类目
$('.allItem').on('click',function(){
	var status = $('.rightShaiXuan').find('b').attr('status');
	var _this = $(this);
	$('.leftShaiXuan').find('b').html($(this).html());
	$('.leftShaiXuan').find('b').attr('categoryId',null);
	orderBy = $('.rightShaiXuan').find('b').attr('orderBy');
	$('#secondPopover').hide();
	$('.firstCategory').hide();
	$('.rightShaiXuan').attr("hide","true");
	$('.leftShaiXuan').attr("hide","true");
	$.ajax({
		type:"POST",
		url : "/maidian/item/itemListMore.htm",
		dataType : "html",
		data : {
			"status" :status,
			"categoryId" :null
		},
		success : function(data) {
			$('.ulWrap').html(data);
	}
	});
})
//商品状态筛选后商品列表
$('#secondPopover').find('li').on('click',function(){
	var status = $(this).attr("status");
	$('#secondPopover').hide();
	$('.rightShaiXuan').find('b').html($(this).html());
	$('.rightShaiXuan').find('b').attr('status',status);
	$('#secondPopover').hide();
	$('.rightShaiXuan').attr("hide","true");
	$('.firstCategory').hide();
	$('.leftShaiXuan').attr("hide","true");
	var categoryId = $('.leftShaiXuan').find('b').attr('categoryId');
	ajaxId = $('.leftShaiXuan').find('b').attr('categoryId');
	$.ajax({
		type:"POST",
		url : "/maidian/item/itemListMore.htm",
		dataType : "html",
		data : {
			"status" :status,
			"categoryId" :categoryId
		},
		success : function(data) {
			$('.ulWrap').html(data);
		}
	})
})

/*//增加商品
$('.addItem').on('click',function(){
	alert(1);
	window.location.href="/weishang/item/addOrModifyItem.htm";
})*/

/*---------------------------上拉加载更多-------------------------------*/
mui.init({
  pullRefresh : {
    container:'#pullrefresh',//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
    up : {
      height:50,//可选.默认50.触发上拉加载拖动距离
      auto:true,//可选,默认false.自动上拉加载一次
      contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
      contentnomore:'没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
      callback: function() {
    	  console.log("======"+ajaxStatus);
    	  console.log(ajaxId);
				var self = this;
				var dataStatus = false;
				var index = document.querySelector('.mui-table-view').getAttribute('index');
				var numObj = document.querySelector('#pageNum');
				var loadMore = document.querySelector('.mui-pull-loading');
				var ul = document.querySelector('.ulWrap');
				var my = document.createElement('my');
				console.log(numObj.value);
				$.ajax({
					url:'/maidian/item/itemListMore.htm',
					type:'get',
					dataType:'html',
					data:{
						'status':ajaxStatus,
						'categoryId':ajaxId,
						'pageNo':numObj.value
					},
					success:function(data){
						console.log(data);
						if(!(data.replace(/(^\s*)|(\s*$)/g,"") == '')){
							numObj.value = numObj.value -(-1);
							my.innerHTML = data;
							ul.appendChild(my);
						}else{
							dataStatus = true;
							loadMore.innerHTML = '没有更多数据了';
						}
					}
				});
				self.endPullupToRefresh(dataStatus);
			}
    }
  }
});
/*mui('body').on('tap','.editWrap',function(e) { 
	e.preventDefault();//防止点透事件
	var mallItemId = $(this).attr('mallItemId');
	window.location.href="/weishang/item/addOrModifyItem.htm?mallItemId="+mallItemId;
});*/
(function($) {
	//页面a标签的点击跳转事件
	mui('body').on('tap', 'a', function (e) {
		if (!this.classList.contains('mui-disabled')) {
		mui.openWindow({
		url: this.getAttribute('href'),
		id: 'info'
		});
		}
		else {
		mui.preventDefault(e);
		mui.stopPropagation(e);
	}
	});
})(mui);
